<?php

/**
 * App
 *
 * @author Lane
 * 
 */
abstract class App {

    public $tplPath;
    public $tplExt = 'html';
    public $db = null;
    public $data;
    public $error;

    abstract protected function getTableName();

    abstract protected function getPrimaryField();

    public function __construct() {
        $this->db = Db::I();
        $this->tplPath = BASEPATH . 'tpl' . DS;
    }

    protected function isAutoIncrement() {
        return true;
    }

    public function table() {
        return $this->db->table($this->getTableName());
    }

    public function insert($data) {
        ($affectedRows = $this->db->table($this->getTableName())->insert($data)) || $this->error = $this->db->getError();
        return $this->db->getLastInsID();
    }

    public function find($id) {
        return $this->table()->where(array($this->getPrimaryField(), 'eq', $id))->find();
    }

    public function delete($id) {
        is_array($id) || $id = array($id);
        return $this->table()->where(array($this->getPrimaryField(), 'in', $id))->delete();
    }

    public function count($where='') {
        $where && $this->db->where($where);
        return $this->table()->count();
    }

    //$type 1 success 2 fail 3 alert
    protected function redirect($url, $msg, $type=1, $wait=2) {
        include $this->tpl('redirect');
        exit;
    }

    protected function tpl($tpl) {
        return $this->tplPath . $tpl . '.' . $this->tplExt;
    }

}

?>
